/**
 * @author KujoMiva
 * @name Tags.ts
 * @desc 底部书签组件
 * @time 2020年03月02日 17:31:32 星期一
 */
class Tags extends egret.DisplayObjectContainer {
  public constructor() {
    super()
    this.once(egret.Event.ADDED_TO_STAGE, this.init, this)
  }

  private init() {
    this.createTagsGroup()

    const btn_right = this.createBtnRight()
    this.addChild(btn_right)
    btn_right.x = 560
    const btn_left = this.createBtnLeft()
    this.addChild(btn_left)
    this.anchorOffsetX = -btn_left.width
  }

  private createTagsGroup() {
    const group = new eui.Group()
    const scrollView = new eui.Scroller()
    this.addChild(scrollView)
    scrollView.viewport = group
    for (const value of DayData.data) {
      const container = new eui.Group()
      const arr = ['b', 'g', 'p', 'y']
      const use_resource = arr[Math.floor(Math.random() * 4)]
      const tag = new eui.Image(RES.getRes(`tab_${use_resource}_png`))
      container.addChild(tag)
      group.addChild(container)
      container.x = 80 * value.id
      const text = new egret.TextField()
      const nowDay = DayData.data[ParamsPool.NOW_PAGE_INDEX + value.id]
      text.text = nowDay.day
      text.size = 24
      text.textColor = 0
      Utils.anchorCenter(text)
      text.x = container.width / 2
      text.y = container.height / 2
      container.addChild(text)
      const active = Utils.createBitmapByName('tab_in_png')
      Utils.anchorCenter(active)
      active.x = container.width / 2
      active.y = container.height / 2
      active.alpha = ParamsPool.NOW_PAGE_INDEX === nowDay.id ? 1 : 0
      container.addChild(active)

      text.addEventListener(egret.Event.ENTER_FRAME, () => {
        active.alpha = ParamsPool.NOW_PAGE_INDEX === nowDay.id ? 1 : 0
      }, this)
      tag.touchEnabled = true
      tag.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
        ParamsPool.NOW_PAGE_INDEX = value.id
        this.stage.dispatchEvent(new egret.Event('tagTap'))
      }, this)
    }
    scrollView.width = 500
  }

  private createBtnLeft() {
    const btn = Utils.createBitmapByName('left_png')
    btn.anchorOffsetX = btn.width
    btn.touchEnabled = true
    btn.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {

    }, this)
    return btn
  }

  private createBtnRight() {
    const btn = Utils.createBitmapByName('right_png')
    btn.anchorOffsetX = btn.width
    return btn
  }
}